# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do

  desc "[CI] Push a new beta build to TestFlight"
  lane :ci_beta do
    setup_ci(
      force: true
    )
    import_certificate(
      certificate_path: "./builds/apple_dist_cert.p12",
      certificate_password: ENV["APPLE_CERTIFICATE_PASSWORD"] || "default",
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"] || "",
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"] || "",
    )
    beta
  end

  desc "[Local] Push a new beta build to TestFlight"
  lane :local_beta do
    get_certificates( # Create or get certificate, and install it
      output_path: "./builds", # Download certificate in the build folder (you don't need to create the folder)
    )
    beta
  end

  desc "Push a new beta build to TestFlight"
  lane :beta do
    get_provisioning_profile( # Create or get provisioning profile
      app_identifier: CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier),
      output_path: "./builds",  # Download provisioning profile in the build folder
      filename: "app.mobileprovision" # Rename the local provisioning profile
    )
    get_provisioning_profile(
      app_identifier: CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier) + ".share-extension",
      output_path: "./builds",
      filename: "share-extension.mobileprovision"
    )
    update_project_provisioning( # Set the project provisioning profile (related in Xcode to the General > Signing Release section)
      xcodeproj: "LikeCoinApp.xcodeproj",
      target_filter: "LikeCoinApp", # Name of your project
      profile: "./builds/app.mobileprovision",
      build_configuration: "Release"
    )
    update_project_provisioning(
      xcodeproj: "LikeCoinApp.xcodeproj",
      target_filter: "LikerLandShare", # Name of your project
      profile: "./builds/share-extension.mobileprovision",
      build_configuration: "Release"
    )
    update_project_team( # Set the right team on your project
      teamid: CredentialsManager::AppfileConfig.try_fetch_value(:team_id)
    )
    build_app(
      workspace: "LikeCoinApp.xcworkspace",
      scheme: "LikeCoinApp",
      clean: true,
      export_method: "app-store",
      export_options: {
        provisioningProfiles: {
          CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier) => CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier) + " AppStore" # Value of this parameter is the name of the Provisioning Profile. By default, it will be "{bundleId} AppStore"
        }
      },
      build_path: "./builds",
      output_directory: "./builds"
    )
    upload_to_testflight(
      skip_waiting_for_build_processing: true,
    )
  end

  desc "Pull dsyms from itunes and upload to crashlytics"
  lane :update_dsyms do
    download_dsyms(
      version: ENV["VERSION_NAME"] || "latest",
      build_number: ENV["BUILD_NUMBER"],
    )
    upload_symbols_to_crashlytics(
      gsp_path: "./Release/GoogleService-Info.plist",
    )
    clean_build_artifacts
  end
end
